﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Silverlight.UI;
using ArtOfTest.WebAii.ObjectModel;
using System.Collections;
using ArtOfTest.WebAii.Core;

namespace SM2DashBoard
{
    public class AddPanelsDialog
    {
        public static void selectCharts(string stringChartName)
        {
            Boolean blFound = false;

            ListBox lb = Utilities.GlobalObject.SILVERLIGHTAPP.Find.ByName<ListBox>("panelDataSources");
            IList<TextBlock> iList = lb.Find.AllByType<TextBlock>();
            int intCountIList = iList.Count - 1; //Because the listbox just can show 14 items as maximize but sometime iList.Count = 15
            string strTemp = "";
            int intIndex = 0;
            for (; intIndex < intCountIList; intIndex++)
            {
                TextBlock tbItem = iList[intIndex];
                strTemp = tbItem.Text;
                //Utilities.GlobalObject.LOGGER.log(strTemp);
                if (strTemp.Equals(stringChartName))
                {
                    tbItem.User.Click();
                    blFound = true;
                    break;
                }
            }
            if (!blFound)
            {
                int intCountListBox = lb.Items.Count;

                int intStep = 1;
                int intAccumulatedItems = intCountIList; // At least, has looped over all items in current view of listbox
                Boolean flag = true;
                while (intAccumulatedItems < intCountListBox)
                {
                    #region
                    ArtOfTest.WebAii.Silverlight.UI.ScrollBar verticalscrollbar = lb.Find.ByName<ArtOfTest.WebAii.Silverlight.UI.ScrollBar>("VerticalScrollBar");
                    if (intStep == 1) verticalscrollbar.User.TurnMouseWheel(1, MouseWheelTurnDirection.Forward, true);
                    else verticalscrollbar.User.TurnMouseWheel(1, MouseWheelTurnDirection.Backward, true);
                    System.Threading.Thread.Sleep(500);

                    Utilities.GlobalObject.SILVERLIGHTAPP.RefreshVisualTrees();
                    lb = Utilities.GlobalObject.SILVERLIGHTAPP.Find.ByName<ListBox>("panelDataSources");
                    iList = lb.Find.AllByType<TextBlock>();

                    intCountIList = iList.Count - 1; //Because the listbox just can show 14 items as maximize but sometime iList.Count = 15
                    for (intIndex = 1; intIndex < 4; intIndex++)
                    {
                        TextBlock tbItem = iList[intIndex];
                        strTemp = tbItem.Text;
                        //Utilities.GlobalObject.LOGGER.log(strTemp);
                        if (strTemp.Equals(stringChartName))
                        {
                            tbItem.User.Click();
                            flag = false; //means has found expected chart name
                            break;
                        }
                    }
                    intAccumulatedItems = intAccumulatedItems + 1;
                    if (!flag) break; //means has found expected chart name

                    #endregion
                }
            }
        }

        public static void selectProfile(string stringProfile)
        {
            Boolean blFound = false;
            string strProfiles = "";
            ListBox lb = Utilities.GlobalObject.SILVERLIGHTAPP.Find.ByName<ListBox>("panelProfiles");
            IList<TextBlock> iList = lb.Find.AllByType<TextBlock>();
            int intCountIList = (iList.Count < 15) ? iList.Count : iList.Count - 1;//Because the listbox just can show 14 items as maximize but sometime iList.Count = 15
            string strTemp = "";
            int intIndex = 0;
            for (; intIndex < intCountIList; intIndex++)
            {
                TextBlock tbItem = iList[intIndex];
                strTemp = tbItem.Text;
                strProfiles = strProfiles + strTemp + ",";

                if (strTemp.Equals(stringProfile))
                {
                    tbItem.User.Click();
                    blFound = true;
                    break;
                }
            }

            if (!blFound)
            {
                int intCountListBox = lb.Items.Count;

                int intStep = 1;
                int intAccumulatedItems = intCountIList; // At least, has looped over all items in current view of listbox
                Boolean flag = true;
                while (intAccumulatedItems < intCountListBox)
                {
                    #region
                    ArtOfTest.WebAii.Silverlight.UI.ScrollBar verticalscrollbar = lb.Find.ByName<ArtOfTest.WebAii.Silverlight.UI.ScrollBar>("VerticalScrollBar");
                    if (intStep++ == 1) verticalscrollbar.User.TurnMouseWheel(1, MouseWheelTurnDirection.Forward, true);
                    else verticalscrollbar.User.TurnMouseWheel(1, MouseWheelTurnDirection.Backward, true);

                    System.Threading.Thread.Sleep(500);
                    Utilities.GlobalObject.SILVERLIGHTAPP.RefreshVisualTrees();

                    lb = Utilities.GlobalObject.SILVERLIGHTAPP.Find.ByName<ListBox>("panelProfiles");
                    iList = lb.Find.AllByType<TextBlock>();
                    intCountIList = iList.Count - 1; //Because the listbox just can show 14 items as maximize but sometime iList.Count = 15
                    for (intIndex = 1; intIndex < 4; intIndex++)
                    {
                        TextBlock tbItem = iList[intIndex];
                        strTemp = tbItem.Text;
                        if (!strProfiles.Contains(strTemp))//This means that we just believe that there are 3 first items are clickable
                        {
                            strProfiles = strProfiles + strTemp + ",";
                        }
                        if (strTemp.Equals(stringProfile))
                        {
                            tbItem.User.Click();
                            flag = false; //means has found expected chart name
                            break;
                        }                        
                    }

                    intAccumulatedItems = intAccumulatedItems + 1;
                    if (!flag) break; //means has found expected chart name

                    #endregion
                }

                //Repead with larger searching --> for (intIndex = 1; intIndex < 9; intIndex++)
                if (flag)
                {
                    intStep = 1;
                    intAccumulatedItems = intCountIList;
                    while (intAccumulatedItems < intCountListBox)
                    {
                        #region
                        ArtOfTest.WebAii.Silverlight.UI.ScrollBar verticalscrollbar = lb.Find.ByName<ArtOfTest.WebAii.Silverlight.UI.ScrollBar>("VerticalScrollBar");
                        if (intStep++ == 1) verticalscrollbar.User.TurnMouseWheel(1, MouseWheelTurnDirection.Forward, true);
                        else verticalscrollbar.User.TurnMouseWheel(1, MouseWheelTurnDirection.Backward, true);

                        System.Threading.Thread.Sleep(500);
                        Utilities.GlobalObject.SILVERLIGHTAPP.RefreshVisualTrees();

                        lb = Utilities.GlobalObject.SILVERLIGHTAPP.Find.ByName<ListBox>("panelProfiles");
                        iList = lb.Find.AllByType<TextBlock>();
                        intCountIList = iList.Count - 1; //Because the listbox just can show 14 items as maximize but sometime iList.Count = 15
                        for (intIndex = 1; intIndex < 13; intIndex++)
                        {
                            TextBlock tbItem = iList[intIndex];
                            strTemp = tbItem.Text;
                            if (!strProfiles.Contains(strTemp))//This means that we just believe that there are 3 first items are clickable
                            {
                                strProfiles = strProfiles + strTemp + ",";
                            }
                            if (strTemp.Equals(stringProfile))
                            {
                                tbItem.User.Click();
                                flag = false; //means has found expected chart name
                                break;
                            }
                        }

                        intAccumulatedItems = intAccumulatedItems + 1;
                        if (!flag) break; //means has found expected chart name

                        #endregion
                    }
                }
            }

            //Save profiles for later using
            Repository.ServerHardDataUtil.saveProfiles(strProfiles);
        }

        public static void closeAddPanels()
        {
            SM2Input.Silverlight.ClickButton("CloseButton");
        }

        public static void clickOK()
        {
            SM2Input.Silverlight.ClickButton("OKButton");
        }

        public static void clickCancel()
        {
            SM2Input.Silverlight.ClickButton("CancelButton");
        }

        public static void clickApply()
        {
            SM2Input.Silverlight.ClickButton("ApplyButton");
        }
    }
}
